#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

//numele programului care executa
char *program_name; 


//Functia de creare de fisier
int creat(const char *pathname, mode_t mode){
	return open(pathname, O_CREAT|O_WRONLY, mode);
}
//Functia de accesare a fisierului creat
int access( const char *path,  int amode){
	  struct stat statbuf;

	  if ( stat(path, &statbuf) )
	    return -1;

	  if ( amode & R_OK ) {
	    if (!( statbuf.st_mode & S_IREAD ))
	      return -1;
	  }

	  if ( amode & W_OK ) {
	    if ( !( statbuf.st_mode & S_IWRITE ) )
	      return -1;
	  }

	  if ( amode & X_OK ) {
	    if ( !( statbuf.st_mode & S_IEXEC ) )
	      return -1;
	  }

  return 0;
}


int main(int argc, char **argv)
{

	program_name = argv[0];
	if (atoi(argv[2])==777){ 	//drepturi de executie
		creat(argv[1], 0777);
	}
	else if(atoi(argv[2])==666){  	//drepturi de scriere
		creat(argv[1], 0666);
	}
	else if(atoi(argv[2])==400){  	//drepturi de citire
		creat(argv[1], 0400);
	}
		
	if (access(argv[1], 1)==0){
		printf("Fisierul are drept de executie\n");
	}
	else if (access(argv[1], 2)==0){
		printf("Fisierul are drept de scriere\n");
	}
	else if (access(argv[1], 4)==0){
		printf("Fisierul are drept de citire\n");
	}
	else if (access(argv[1], 0)==0){
		printf("Fisierul exista\n");
	}
}
		
		
		
		

